package org.jeecg.modules.demo.cashier;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.mall.api.MallHelloApi;
import org.jeecg.modules.demo.mall.entity.YxStoreProduct;
import org.jeecg.modules.demo.weixin.entity.SmtOnceCardInfo;
import org.jeecg.modules.demo.weixin.smtoncecardinfo.service.ISmtOnceCardInfoService;
import org.jeecg.modules.demo.weixin.smtoncecardusers.service.ISmtOnceCardUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author: zhanglu
 * TODO
 * 收银次卡
 * @date 2023/10/28  10:46
 * Description: 类
 */
@Api(tags = "收银次卡")
@Slf4j
@RestController
@RequestMapping("/weixin/cashierOnceCardInfo/cashieroncecardinfo")
public class CashierOnceCardInfo {

    @Autowired
    private ISmtOnceCardUsersService smtOnceCardUsersService;

    @Autowired
    private ISmtOnceCardInfoService smtOnceCardInfoService;
    @Autowired
    private MallHelloApi mallHelloApi;

    /**
     * 分页列表查询
     *
     * @param smtOnceCardInfo //     * @param current
     *                        //     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "smt_once_card_info-分页列表查询")
    @ApiOperation(value = "smt_once_card_info-分页列表查询", notes = "smt_once_card_info-分页列表查询")
    @GetMapping(value = "/queryCardPage")
    public Result<List<SmtOnceCardInfo>> queryPageList(SmtOnceCardInfo smtOnceCardInfo,
//                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer current,
//                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                       HttpServletRequest req) {

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        QueryWrapper<SmtOnceCardInfo> queryWrapper = QueryGenerator.initQueryWrapper(smtOnceCardInfo, req.getParameterMap());
//        Page<SmtOnceCardInfo> page = new Page<SmtOnceCardInfo>(current, pageSize);
//        IPage<SmtOnceCardInfo> pageList = smtOnceCardInfoService.page(page, queryWrapper);
        List<SmtOnceCardInfo> pageList = smtOnceCardInfoService.list(queryWrapper);
        if (CollectionUtils.isEmpty(pageList)) {
            return Result.OK(pageList);
        }
//        pageList.forEach(smtOnceCardInfo1 -> {
//            if (smtOnceCardInfo1.getGoodsId() != null) {
//                YxStoreProduct yxStoreProduct = mallHelloApi.queryName(Integer.valueOf(smtOnceCardInfo1.getGoodsId()));
//                if (yxStoreProduct != null) {
//                    smtOnceCardInfo1.setGoodsName(yxStoreProduct.getStoreName());
//                }
//            }
//        });


        List<SmtOnceCardInfo> counts = new ArrayList<>();
        Stream<SmtOnceCardInfo> smtOnceCardStream = pageList.stream().filter(smtOnceCard ->
                smtOnceCard.getCardValidDate().equals("无限制")
        );
        if (smtOnceCardStream != null) {
            List<SmtOnceCardInfo> onceCardUsers = smtOnceCardStream.map(card1 -> {
                if (card1.getGoodsId() != null) {
                    YxStoreProduct yxStoreProduct = mallHelloApi.queryName(Integer.valueOf(card1.getGoodsId()));
                    if (yxStoreProduct != null) {
                        card1.setGoodsName(yxStoreProduct.getStoreName());
                    }
                }
                return card1;
            }).collect(Collectors.toList());

            counts.addAll(onceCardUsers);
        }


        pageList.forEach(card -> {
            if (!card.getCardValidDate().equals("无限制")) {
                if (LocalDate.parse(card.getCardValidDate(), dateTimeFormatter).compareTo(LocalDate.now()) > 0) {
                    if (card.getGoodsId()!=null) {
                        YxStoreProduct yxStoreProduct = mallHelloApi.queryName(Integer.valueOf(card.getGoodsId()));
                        if (yxStoreProduct != null) {
                            card.setGoodsName(yxStoreProduct.getStoreName());
                        }
                    }
                    counts.add(card);
                }
            }
            //次卡只是查询可用的  但是没有修改 如果要修改可在家字段   直接变成已过期
        });
        return Result.OK(counts);

    }
}
